package com;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.*;

public class MiniDbutils {

    public static void close(Connection con) throws SQLException {
        if(con != null){
            con.close();
        }
    }

    public static void close(ResultSet rs) throws SQLException {
        if(rs != null){
            rs.close();
        }
    }

    public static void close(Statement st) throws SQLException {
        if(st != null){
            st.close();
        }
    }

    /*下面是安静的关闭*/
    //报错了自己吞着

    public static void closeQuitely(Connection con){
        try {
            close(con);//方法的其中一个作用，减少重复代码，可以重复利用不为null的判断逻辑
        } catch (SQLException throwables) {
            // 如果出了错，直接捕获不做任何处理，
            // 这会导致调用此方法的地方不知道发生了错误
        }
    }

    public static void closeQuitely(Statement st){
        try {
            close(st);
        } catch (SQLException throwables) {

        }
    }

    public static void closeQuitely(ResultSet rs){
        try {
            rs.close();
        } catch (SQLException throwables) {

        }
    }

    public static void closeQuitely(Connection con, Statement st, ResultSet rs){
        try{
            closeQuitely(rs);
        }finally {
            try{
                closeQuitely(st);
            }finally {
                closeQuitely(con);
            }
        }
    }
}
